//
// Created by 邹嘉旭 on 2025/8/5.
//

#include "elements/dbsm.h"
#include "define.h"
#include "interface/ground.h"
#include "util/log.h"


dbsm_entity_t *init_dbsm_entity() {
    dbsm_entity_t *dbsm_entity = malloc(sizeof(dbsm_entity_t));

    return dbsm_entity;
}

void free_dbsm_entity(dbsm_entity_t *entity) {
    free(entity);
}

/**
 * DBSM 服务端业务（G7接口）
 * @param bc
 * @return
 */
static l_err dbsm_server_handler(basic_conn_t *bc) {
    if (!bc || !bc->read_pkt ) {
        log_error("DBSM server recv failed");
        return LD_ERR_NULL;
    }
    element_propt_t *element_propt = (element_propt_t *) bc;

    net_ctx_t *ctx = bc->opt;
    meta_service_t *meta = ctx->arg;

    // cn_log_buf(LOG_INFO, meta->net_ele->element_tag, meta->interface_type, "RECV DBSM", element_propt->bc.read_pkt->ptr,
    //            element_propt->bc.read_pkt->len);

    uint8_t type = *element_propt->bc.read_pkt->ptr;
    cn_format_desc_t *recv_desc = get_non_sequential_desc(g7_format_size, g7_format_descs, type);
    if (!recv_desc) {
        cn_log_error(meta->net_ele->element_tag, meta->interface_type, "No desc");
        return LD_ERR_NULL;
    }

    if (recv_desc->recv_handler) {
        if (recv_desc->recv_handler(meta->net_ele, recv_desc, element_propt->bc.read_pkt, element_propt) != LD_OK) {
            cn_log_error(meta->net_ele->element_tag, meta->interface_type, "Handle Message Failed!");
            return LD_ERR_INVALID;
        }
    }

    return LD_OK;
}

/**
 * DBSM 客户端业务（G5接口）
 * @param bc
 * @return
 */
static l_err dbsm_client_handler(basic_conn_t *bc) {
    if (!bc || !bc->read_pkt ) {
        log_error("DBSM client recv failed");
        return LD_ERR_NULL;
    }
    element_propt_t *element_propt = (element_propt_t *) bc;

    net_ctx_t *ctx = bc->opt;
    meta_service_t *meta = ctx->arg;

    // cn_log_buf(LOG_INFO, meta->net_ele->element_tag, meta->interface_type, "RECV DBSM", element_propt->bc.read_pkt->ptr,
    //            element_propt->bc.read_pkt->len);
    uint8_t type = *element_propt->bc.read_pkt->ptr ;

    cn_format_desc_t *recv_desc = get_non_sequential_desc(g5_format_size, g5_format_descs, type);
    if (!recv_desc) {
        cn_log_error(meta->net_ele->element_tag, meta->interface_type, "No desc for 0x%02x", type);
        return LD_ERR_NULL;
    }

    if (recv_desc->recv_handler) {
        if (recv_desc->recv_handler(meta->net_ele, recv_desc, element_propt->bc.read_pkt, element_propt) != LD_OK) {
            cn_log_error(meta->net_ele->element_tag, meta->interface_type, "Handle Message Failed!");
            return LD_ERR_INVALID;
        }
    }else {
        cn_log_error(meta->net_ele->element_tag, meta->interface_type, "No handler");
        return LD_ERR_INVALID;
    }

    return LD_OK;
}

void *exec_dbsm(void *args) {
    net_element_t *net_ele = args;
    if (!net_ele)   return NULL;
    net_ele->inline_element = init_dbsm_entity();
    if (init_element_service(net_ele, dbsm_server_handler, dbsm_client_handler) != LD_OK) {
        return NULL;
    }

    return NULL;
}

l_err start_dbsm(net_element_t *net_ele) {
    cn_log_info(net_ele->element_tag, INTERFACE_UNDEFINED, "start");
    pthread_create(&net_ele->th, NULL, exec_dbsm, net_ele);
    pthread_detach(net_ele->th);
    return LD_OK;
}


l_err stop_dbsm(net_element_t *net_ele) {
    cn_log_error(net_ele->element_tag, INTERFACE_UNDEFINED, "stop");
    if (net_ele->inline_element) {
        free_dbsm_entity(net_ele->inline_element);
    }
    return LD_OK;
}
